var app = getApp();
// 引入SDK核心类
var QQMapWX = require('../../lib/qqmap-wx-jssdk.min.js');
Page({
  data: {
    //获取地理位置窗口内容
    locationErrContent: '为了防止虚假发布送养信息,我们需要获取您的地理位置.',
    locationErrHidden: true,
    cityList: [],
    giveCity: '未定位',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    //找到系统状态位置
    this.setData({
      statusBarHeight: app.globalData.systemInfo.statusBarHeight + 15
    })
    if (app.globalData.location != null) {
      this.setData({
        giveCity: app.globalData.location.city
      })
    }
    this.getCityList();
  },
  orientation: function(e) {
    var giveCity = this.data.giveCity;
    if (giveCity == '未定位') {
      return;
    }else{
      var pages = getCurrentPages();
      var page = pages[pages.length - 2];
      page.setData({
        giveCity: giveCity,
        refresh: true
      })
      page.getGivePetListByCity();
      this.rollback();
    }
  },
  //切换地区
  selCity: function(e) {
    var index = e.currentTarget.dataset.index;
    var city = this.data.cityList[index].name;
    var pages = getCurrentPages();
    var page = pages[pages.length - 2];
    page.setData({
      giveCity: city,
      refresh: true
    })
    page.getGivePetListByCity();
    this.rollback();
  },

  //获取城市列表
  getCityList: function() {
    var that = this;
    wx.request({
      url: app.globalData.requestUrl + 'getCityList',
      success: function(r) {
        if (r.data.code == 200) {
          that.setData({
            cityList: r.data.data
          })
        } else {
          wx.showToast({
            title: '获取热门城市失败',
            duration: '3000',
            icon: 'none'
          })
        }
      },
      fail: function() {
        wx.showToast({
          title: '网络断开连接,获取热门城市失败',
          duration: '3000',
          icon: 'none'
        })
      }
    })
  },

  getLocation: function() {
    var that = this;
    wx.getLocation({
      success: function(r) {
        // 调用接口
        // 实例化API核心类
        var map = new QQMapWX({
          key: '7KHBZ-DSTWP-UOCD7-V4ABM-LJH2F-G4BAJ' // 必填
        });
        map.reverseGeocoder({
          location: {
            latitude: r.latitude,
            longitude: r.longitude,
          },
          success: function(r) {
            var location = {
              province: r.result.address_component.province,
              city: r.result.address_component.city,
              district: r.result.address_component.district,
              address: r.result.address
            }
            app.globalData.location = location;
            that.setData({
              giveCity: location.city
            })
          }
        });
      },
      fail: function() {
        that.setData({
          locationErrHidden: false
        })
      }
    })
  },
  //打开授权页
  openSetting: function(e) {
    var scope = e.detail.detail.authSetting;
    if (scope['scope.userLocation']) {
      this.getLocation();
      this.setData({
        locationErrHidden: true
      })
    }
  },
  //返回下一个页面
  rollback: function() {
    wx.navigateBack({
      delta: 1
    })
  }
})